package chapterSystem.client.interfaceDrawing.activities;

import chapterSystem.client.ChapterSystemService;
import chapterSystem.client.callbacks.WriteSimpleEntityCallback;
import chapterSystem.client.interfaceDrawing.InterfaceProvider;
import chapterSystem.client.model.entities.ChapterEventEntity;
import chapterSystem.client.model.entitiesInt.Brother;
import chapterSystem.client.model.enums.ActivityType;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;

import java.util.Date;

/**
 * User: Hasp
 * Date: 06.05.12
 * Time: 21:01
 */
public class CreateEventActivity extends Activity {
    private final Date myDate;
    private final Activity callback;

    protected CreateEventActivity(final Brother brother, final ActivityType type, Date myDate, Activity callback) {
        super(brother, type);
        this.myDate = myDate;
        this.callback = callback;
    }

    @Override
    public void drawActivity(final StackLayoutPanel workArea) {
        workArea.clear();
        final VerticalPanel mainPanel = new VerticalPanel();
        mainPanel.setWidth("100%");
        mainPanel.setHeight("100%");

        final HorizontalPanel titlePanel = new HorizontalPanel();
        titlePanel.setWidth("100%");
        titlePanel.add(new Label("Title: "));
        final TextArea titleField = InterfaceProvider.getInstance().createTextField();
        titlePanel.add(titleField);

        final TextArea textArea = new TextArea();
        textArea.setWidth("100%");
        textArea.setHeight("100%");

        final HorizontalPanel buttonPanel = new HorizontalPanel();
        buttonPanel.setWidth("100%");
        final Button submit = new Button("Create");
        final Button back = new Button("<< Back");

        submit.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                if (titleField.getText().trim().length() == 0) {
                    Window.alert("Title must not be empty");
                } else if (textArea.getText().trim().length() == 0) {
                    Window.alert("Description must not be empty");
                } else {
                    ChapterSystemService.App.getInstance().createEvent(new ChapterEventEntity(0, titleField.getText(),
                            textArea.getText(), myDate), new WriteSimpleEntityCallback(workArea, callback));
                }
            }
        });
        back.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                callback.drawActivity(workArea);
            }
        });

        buttonPanel.add(submit);
        buttonPanel.add(back);

        mainPanel.add(titlePanel);
        mainPanel.add(textArea);
        mainPanel.add(buttonPanel);

        mainPanel.setCellHeight(titlePanel, "25px");
        mainPanel.setCellHeight(buttonPanel, "25px");

        workArea.add(mainPanel, new HTML("Create new event at " + myDate.toString()), 4);
    }
}
